package com.topisv.tms.service.stock;

import java.text.DecimalFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.stock.StockBoxCode;
import com.topisv.tms.repository.stock.StockBoxCodeDao;

/**
 * 存储箱条码_service
 * @author ch
 *
 */
@Component
@Transactional(readOnly = true)
public class StockBoxCodeService {
	
	@Autowired
	private StockBoxCodeDao stockBoxCodeDao;

	/**
	 * 查询
	 * @param customerCode 商家编码
	 * @param warehouseCode 仓库编码
	 * @return
	 * @throws Exception 
	 */
	@Transactional(readOnly = false)
	public StockBoxCode loadByCondition(String customerCode,String warehouseCode) throws Exception {
	     try {
			
			DecimalFormat df=new DecimalFormat("00000");
			StockBoxCode stockBoxCode=this.stockBoxCodeDao.loadByCondition(customerCode,warehouseCode);
			if(stockBoxCode==null){
				stockBoxCode=new StockBoxCode();
				stockBoxCode.setCustomerCode(customerCode);
				stockBoxCode.setWarehouseCode(warehouseCode);
				stockBoxCode.setCurRankno(1);
				stockBoxCode.setCurCode("B"+warehouseCode+df.format(1));
				this.stockBoxCodeDao.save(stockBoxCode);
			}
			return stockBoxCode;
			
	     } catch (Exception e) {
	    	 throw e;
	 	 }
	}

	@Transactional(readOnly = false)
	public void updateRankno(String customerCode, String warehouseCode, int num) {
		DecimalFormat df=new DecimalFormat("00000");
		StockBoxCode stockBoxCode=this.stockBoxCodeDao.loadByCondition(customerCode,warehouseCode);
		int curRankno=stockBoxCode.getCurRankno()+num;
		stockBoxCode.setCurCode("B"+warehouseCode+df.format(curRankno));
		stockBoxCode.setCurRankno(curRankno);
		this.stockBoxCodeDao.save(stockBoxCode);
	}
	


}
